package skin;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

public class SkinAnalysis {
    private static final String API_KEY = "3UznLENkCCGHI1VBxtqlfzxy2Rg6zv6J";
    private static final String SECRET_KEY = "g3iL_gem_67PeKi96nVUGNinjDYeZGqn";
    // 高级版
    private static final String API_URL = "https://api-cn.faceplusplus.com/facepp/v1/skinanalyze_advanced";

    public static void main(String[] args) {
        // 图片路径
        File imageFile = new File("C:\\Users\\Administrator\\Pictures\\mz\\demo-pic-skinAnalysis-1.jpg");
        try {
            String result = analyzeSkin(imageFile);
            String parsedResult = parseResultToChinese(result);
            // 指定输出流的编码为 UTF-8
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out, StandardCharsets.UTF_8));
            writer.println(parsedResult);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String analyzeSkin(File imageFile) throws IOException {
        HttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(API_URL);

        // 指定请求头的 Accept-Charset 为 UTF-8
        httpPost.setHeader("Accept-Charset", "UTF-8");
        // 同时设置 Content-Type 的字符编码为 UTF-8
        httpPost.setHeader("Content-Type", "multipart/form-data; charset=UTF-8");


        // 添加请求参数
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("api_key", API_KEY);
        builder.addTextBody("api_secret", SECRET_KEY);
        builder.addBinaryBody("image_file", imageFile, ContentType.APPLICATION_OCTET_STREAM, imageFile.getName());

        HttpEntity multipart = builder.build();
        httpPost.setEntity(multipart);

        // 执行请求
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity responseEntity = response.getEntity();

        // 获取响应结果并指定编码为 UTF-8
        String responseString = EntityUtils.toString(responseEntity, "UTF-8");

        // 打印响应头信息，用于调试
        System.out.println("Response Headers:");
        for (org.apache.http.Header header : response.getAllHeaders()) {
            System.out.println(header.getName() + ": " + header.getValue());
        }

        // 解析 JSON 并获取 "result" 部分
        JSONObject jsonObject = new JSONObject(responseString);
        JSONObject resultObject = jsonObject.optJSONObject("result"); // 使用 optJSONObject 避免空指针

        return resultObject != null ? resultObject.toString() : "{}"; // 如果 result 为空，返回空 JSON
    }

    public static String parseResultToChinese(String result) {
        JSONObject jsonObject = new JSONObject(result);

        StringBuilder chineseResult = new StringBuilder();
        chineseResult.append("皮肤分析结果如下：\n");

        // 油性程度
        int oil = jsonObject.optInt("oil", -1);
        if (oil != -1) {
            chineseResult.append("油性程度：").append(getOilDescription(oil)).append("\n");
        } else {
            chineseResult.append("油性程度：未知\n");
        }

        // 毛孔状况
        int pores = jsonObject.optInt("pores", -1);
        if (pores != -1) {
            chineseResult.append("毛孔状况：").append(getPoresDescription(pores)).append("\n");
        } else {
            chineseResult.append("毛孔状况：未知\n");
        }

        // 纹理状况
        int wrinkles = jsonObject.optInt("wrinkles", -1);
        if (wrinkles != -1) {
            chineseResult.append("纹理状况：").append(getWrinklesDescription(wrinkles)).append("\n");
        } else {
            chineseResult.append("纹理状况：未知\n");
        }

        // 黑眼圈程度
        int darkCircles = jsonObject.optInt("dark_circles", -1);
        if (darkCircles != -1) {
            chineseResult.append("黑眼圈程度：").append(getDarkCirclesDescription(darkCircles)).append("\n");
        } else {
            chineseResult.append("黑眼圈程度：未知\n");
        }

        // 痘痘状况
        int acne = jsonObject.optInt("acne", -1);
        if (acne != -1) {
            chineseResult.append("痘痘状况：").append(getAcneDescription(acne)).append("\n");
        } else {
            chineseResult.append("痘痘状况：未知\n");
        }

        return chineseResult.toString();
    }

    // 油性程度描述
    private static String getOilDescription(int oil) {
        if (oil < 30) return "偏干";
        else if (oil < 60) return "正常";
        else return "偏油";
    }

    // 毛孔状况描述
    private static String getPoresDescription(int pores) {
        if (pores < 30) return "毛孔细腻";
        else if (pores < 60) return "毛孔一般";
        else return "毛孔粗大";
    }

    // 纹理状况描述
    private static String getWrinklesDescription(int wrinkles) {
        if (wrinkles < 30) return "纹理光滑";
        else if (wrinkles < 60) return "纹理一般";
        else return "纹理粗糙";
    }

    // 黑眼圈程度描述
    private static String getDarkCirclesDescription(int darkCircles) {
        if (darkCircles < 30) return "无明显黑眼圈";
        else if (darkCircles < 60) return "轻微黑眼圈";
        else return "严重黑眼圈";
    }

    // 痘痘状况描述
    private static String getAcneDescription(int acne) {
        if (acne < 30) return "无明显痘痘";
        else if (acne < 60) return "轻微痘痘";
        else return "严重痘痘";
    }
}